<template>
  <div class="content-box">
    <!-- 登录主体 -->
    <div class="login-content">
      <!-- logo -->
      <div class="login-content-logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        label-width="0px"
        class="login-form"
        :model="loginForm"
        ref="loginFormRef"
        :rules="loginFormRules"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item class="login-form-btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单数据对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置按钮
    reset() {
      this.$refs.loginFormRef.resetFields()
    },
    // 提交表单
    login() {
      this.$refs.loginFormRef.validate(async validata => {
        // 判断提交表单之前是否能通过预验证
        if (!validata) return false
        // 发送请求向后台获取验证
        const { data: res } = await this.$http.post('login', this.loginForm)
        console.log(res)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        // 提示登录成功
        this.$message.success(res.meta.msg)
        // 设置token保存到浏览器中方便登录其他页面
        window.sessionStorage.setItem('token', res.data.token)
        // 编程式导航跳转到首页
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped lang="less">
.content-box {
  height: 100%;
  background-color: #2b4b6b;
}

.login-content {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  transform: translate(-50%, -50%);
  .login-content-logo {
    position: absolute;
    top: 0;
    left: 50%;
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    background-color: #fff;
    box-shadow: 0 0 10px 3px #eee;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .login-form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    .login-form-btn {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
